package com.thot.timyag.config.properties;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import lombok.Getter;
import lombok.Setter;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

/**
 * @author yangjf
 * @version 0.1.0
 * @time 2019/4/25
 */
@ConfigurationProperties("thot.mybatis")
@Getter
@Setter
public class ThotMyBatisProperties {
    private boolean enable;
    private String[] mapperLocations = new String[]{"classpath*:mapper/**/*.xml"};

    private String[] basePackages = new String[]{""};

    public Resource[] resolveMapperLocations(ClassLoader cl) {
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(cl);
        List<Resource> resources = new ArrayList();
        if (this.mapperLocations != null) {
            String[] locations = this.mapperLocations;
            for (int i = 0; i < locations.length; ++i) {
                String mapperLocation = locations[i];

                try {
                    Resource[] mappers = resourceResolver.getResources(mapperLocation);
                    resources.addAll(Arrays.asList(mappers));
                } catch (IOException var8) {

                }
            }
        }

        return (Resource[]) resources.toArray(new Resource[resources.size()]);
    }

}
